package com.jcfk.eam.controller.mould;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.api.MdmServiceFegin;
import com.jcfk.eam.domain.dto.main.EamPointCheckResultDTO;
import com.jcfk.eam.domain.dto.mould.MouldLedgerOperateDTO;
import com.jcfk.eam.domain.dto.mould.MdmMaterialDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.service.base.EamSopStandardService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import com.jcfk.eam.service.mould.MouldAssetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 通用模具台账管理
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-11-13
 */
@Api(tags = {"模具台账管理"})
@RestController
@RequestMapping("eammodmanage")
public class MouldManageController {

    @Autowired
    private MdmServiceFegin mdmServiceFegin;

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    @Autowired
    private EamSopStandardService eamSopStandardService;


    @Autowired
    private MouldAssetInfoService mouldAssetInfoService;

    /**
     * 获取模具物料信息、模具信息及尺寸点检标准
     */
    @ApiOperation("查询通用模具台账管理")
    @GetMapping("/getMdmmaterial/{code}")
    public RetResult<MouldLedgerOperateDTO> getMdmMaterial(@PathVariable("code") String code) {
        // 获取模具物料信息
        MouldLedgerOperateDTO dto = new MouldLedgerOperateDTO();
        String sapCode = code.substring(0, 7);
        dto.setSapCode(sapCode);
        String projectCode = code.substring(7, 11);
        dto.setProjectCode(projectCode);
        String dataString = code.substring(11, 19);
        dto.setDataString(dataString);
        String processCode = code.substring(code.length() - 3);
        dto.setProcessCode(processCode);
        dto.setCode(code);
        RetResult<MdmMaterialDTO> mdmMaterialDTORetResult = mdmServiceFegin.getMdmmaterial(sapCode);

        if (!mdmMaterialDTORetResult.getCode().equals(200) || mdmMaterialDTORetResult.getData() == null) {
            throw new CustomException("根据该模具编码未检索到模具物料!");
        }

        // 尺寸点检标准
        EamPointCheckResultDTO eamSopStandardDTO = eamSopStandardService.getSopStandard("moldEnterInspect", sapCode);

        dto.setMdmMaterialDTO(mdmMaterialDTORetResult.getData());
        dto.setEamSopStandardDTO(eamSopStandardDTO);

        // 获取模具台账信息
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetInfo::getAssetCode, code);
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getOne(queryWrapper);
        dto.setEamAssetInfo(eamAssetInfo);
        // 8206139 2714 20240522 001
        return RetResult.ok(dto);
    }


    /**
     * 入库
     */
    @ApiOperation("入库")
    @PostMapping("/save")
    public RetResult<?> inStock(@RequestBody MouldLedgerOperateDTO dto) {
        RetResult<?> ret = mouldAssetInfoService.inStock(dto);
        return ret;
    }

    /**
     * 查询模具台账信息
     */
    @ApiOperation("查询模具台账信息")
    @GetMapping("/getEamAssetInfo/{code}")
    public RetResult<MouldLedgerOperateDTO> getEamAssetInfo(@PathVariable("code") String code) {
        MouldLedgerOperateDTO dto = new MouldLedgerOperateDTO();
        dto.setCode(code);
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetInfo::getAssetCode, code);
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getOne(queryWrapper);
        dto.setEamAssetInfo(eamAssetInfo);

        if (eamAssetInfo == null) {
            throw new CustomException("根据该模具编码未检索到模具记录!");
        }

        String sapCode = code.substring(0, 7);
        RetResult<MdmMaterialDTO> mdmMaterialDTORetResult = mdmServiceFegin.getMdmmaterial(sapCode);
        dto.setMdmMaterialDTO(mdmMaterialDTORetResult.getData());

//        if (eamAssetInfo.getStorageStatus() == 3) {
//            throw new CustomException("该工装库存状态已经到设备，无需再操作!");
//        }

        return RetResult.ok(dto);
    }

    /**
     * 出库
     */
    @ApiOperation("出库")
    @PostMapping("/out")
    public RetResult<?> outStock(@RequestBody MouldLedgerOperateDTO dto) {
        RetResult<?> ret = mouldAssetInfoService.outStock(dto);
        return ret;
    }

}
